package com.game;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CartDAO {
    // 添加游戏到购物车（如果已存在则更新数量）
    public void addToCart(int userId, int gameId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();

            // 检查是否已有该游戏在购物车
            String checkSql = "SELECT * FROM carts WHERE user_id = ? AND game_id = ?";
            stmt = conn.prepareStatement(checkSql);
            stmt.setInt(1, userId);
            stmt.setInt(2, gameId);
            rs = stmt.executeQuery();

            if (rs.next()) {
                // 更新现有记录数量
                String updateSql = "UPDATE carts SET quantity = quantity + 1 WHERE user_id = ? AND game_id = ?";
                stmt = conn.prepareStatement(updateSql);
                stmt.setInt(1, userId);
                stmt.setInt(2, gameId);
                stmt.executeUpdate();
            } else {
                // 插入新记录
                String insertSql = "INSERT INTO carts (user_id, game_id, quantity) VALUES (?, ?, 1)";
                stmt = conn.prepareStatement(insertSql);
                stmt.setInt(1, userId);
                stmt.setInt(2, gameId);
                stmt.executeUpdate();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(conn, stmt, rs);
        }
    }

    // 获取用户的购物车商品列表
    public List<Game> getCartItems(int userId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Game> cartItems = new ArrayList<>();
        try {
            conn = DBHelper.getConnection();
            String sql = "SELECT g.*, c.quantity " +
                    "FROM games g " +
                    "INNER JOIN carts c ON g.id = c.game_id " +
                    "WHERE c.user_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            rs = stmt.executeQuery();

            while (rs.next()) {
                Game game = new Game();
                game.setId(rs.getInt("id"));
                game.setName(rs.getString("name"));
                game.setPrice(rs.getDouble("price"));
                game.setQuantity(rs.getInt("quantity")); // 自定义添加quantity属性
                cartItems.add(game);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(conn, stmt, rs);
        }
        return cartItems;
    }

    // 更新购物车商品数量
    public void updateCartItem(int userId, int gameId, int quantity) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBHelper.getConnection();
            String sql = "UPDATE carts SET quantity = ? WHERE user_id = ? AND game_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, quantity);
            stmt.setInt(2, userId);
            stmt.setInt(3, gameId);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(conn, stmt, null);
        }
    }

    // 移除购物车中的商品
    public void removeCartItem(int userId, int gameId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBHelper.getConnection();
            String sql = "DELETE FROM carts WHERE user_id = ? AND game_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            stmt.setInt(2, gameId);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(conn, stmt, null);
        }
    }
}